package com.theanh.RssActivity;

import android.content.Context;
import android.util.Log;

import com.theanh.RssActivity.db.DbAdapter;
import com.theanh.RssActivity.entity.Category;
import com.theanh.RssActivity.entity.Channel;
import com.theanh.RssActivity.entity.Setting;
import com.theanh.RssActivity.ui.model.CategoryModel;
import com.theanh.RssActivity.ui.model.ChannelModel;
import com.theanh.RssActivity.util.Constants;
import com.theanh.RssActivity.util.DbUtils;
import com.theanh.RssActivity.util.FileUtils;

public class Store {
	private static final String TAG = "Store";
	private static Store instance = null;
	public static Context context;
	public static DbAdapter dbAdapter;

	public static ChannelModel channelModelSelected;
	public static CategoryModel categoryModelSelected;
	public static boolean hasCategoryFocus = false;
	public static boolean hasChannelFocus = false;

	public static Setting setting = new Setting();

	// Constructor private
	private Store() {
		dbAdapter = new DbAdapter(context);
	}

	public static void init() {
		if (context != null)
			dbAdapter = new DbAdapter(context);

		// Load setting information
		DbAdapter dbAdapter = null;
		try {
			dbAdapter = DbUtils.createAndOpenDbAdapter();
			setting = dbAdapter.getSetting();
		} catch (Exception ex) {
			Log.e(TAG, ex.toString());

			// TODO Show error message
		} finally {
			DbUtils.closeDbAdapter(dbAdapter);
		}
		
		// Create image folders
		FileUtils.createDirectory(Constants.HOME_DIR);
		FileUtils.createDirectory(Constants.IMAGES_DIRECTORY);
		FileUtils.createDirectory(Constants.EXTERNAL_HOME_DIR);
		FileUtils.createDirectory(Constants.EXTERNAL_IMAGES_DIRECTORY);
	}

	public static Store getInstance() {
		if (instance == null) {
			// Initiate instance
			instance = new Store();
		}
		return instance;
	}
}
